# FileGuess - checks binary files for certain signatures to find out
# their mimetype.
# Copyright 2007, Rudolf Olah <omouse@gmail.com>
# Licensed under the New BSD license

import mimetypes

SIGNATURE_MP3 = '\x49\x44\x33'
SIGNATURE_OGG = '\x4F\x67\x67'

def guess_mimetype (filename):
    """ Looks for signature of a binary file to identify its
    mime-type. Returns the mime-type string."""
    f = open (filename, 'rb')
    s = f.read (50)
    bytesRead = 50
    while bytesRead < 300:
#        print s
        if s.find (SIGNATURE_MP3) > -1:
            return 'audio/mpeg'
        if s.find (SIGNATURE_OGG) > -1:
            return 'application/ogg'
        s = f.read (50)
        bytesRead += 50
    return ''

def is_mimetype (filename, mimetype):
    """ Looks for the signature of a binary file and if it matches the
    mimetype given, returns True"""
    return guess_mimetype (filename) == mimetype


# Test files are needed obviously
#print 'test.mp3', guess_mimetype ('test.mp3')
#print 'test.ogg', guess_mimetype ('test.ogg')
print guess_mimetype ('/home/omouse/me.jpg')
